import React, { Component } from 'react';
import { login } from '../../api' 
class Index extends Component {
  state = {
    user: '',
    pwd: ''
  }
  async handleLogin(data) {
    const resp = await login(data)
    const { code, token } = resp.data
    if(code === 200) {
      localStorage.setItem('token', token)
      this.props.history.push('/')
    } else {
      alert('账户密码错误！！！')
    }
  }
  render() {
    const { user, pwd } = this.state
    return (
      <div>
        <form>
          <p>
            <b>账户：</b>
            <input type="text" value={ user } 
              onChange={ (e) => { this.setState({ user: e.target.value  }) }}
            />
          </p>
          <p>
            <b>密码：</b>
            <input type="password" value={ pwd }
              onChange={ (e) => { this.setState({ pwd: e.target.value  }) }}
            />
          </p>
          <p>
            <button type="button" onClick={ () => this.handleLogin({ user,pwd }) }>登录</button>
          </p>
        </form>
      </div>
    );
  }
}

export default Index;
